<template>
  <div>
    
    <hr />
    <el-table
      :data="tableData"
      border
      style="width: 100%; margin-top: 20px; margin-bottom: 20px"
    >
      <el-table-column prop="_id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="subjectsId.name" label="所属专业">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageData.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
     
      pageData: {
        currentPage: 1,
        pageSize: 5,
      },
      total: 0,
    };
  },
  created() {
    this.getTeachers();
  },
  watch: {
    pageData: {
      handler() {
        this.getTeachers();
      },
      deep: true,
    },
  },
  methods: {
    //点击跳转到修改页面，并传参
    handleEdit(row) {
      this.$router.push(`/home/teacherUpdate/${row._id}`);
    },

    //获取，渲染全部老师
    async getTeachers() {
      const res = await this.$api.teachers.get(this.pageData);

      if (res.code) {
        console.log(res.data.rows);
        this.tableData = res.data.rows;
        this.total = res.data.total;
      }
    },
    //删除老师
    async handleDelete(row) {
      const res = await this.$api.teachers.delete({ _id: row._id });
      console.log(row);
      if (res.code) {
        console.log(res);
        this.$message({
          message: "删除班级成功",
          type: "success",
        });
        this.getTeachers();
      } else {
        this.$message.error("删除班级失败");
      }
    },

    //选择每页显示条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageData.pageSize = val;
    },
    //选择页码
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageData.currentPage = val;
    },
  },
};
</script>

<style></style>
